package com.situ.feemanagement.controller;

import com.situ.feemanagement.pojo.dto.UserPasswordDTO;
import com.situ.feemanagement.pojo.entity.User;
import com.situ.feemanagement.pojo.dto.UserLoginDTO;
import com.situ.feemanagement.pojo.query.UserQuery;
import com.situ.feemanagement.service.IUserService;
import com.situ.feemanagement.util.CommonUtil;
import com.situ.feemanagement.util.JwtUtil;
import com.situ.feemanagement.util.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    public Result list(UserQuery userQuery) {
        Map<String, Object> map = userService.selectByPage(userQuery);
        return Result.ok(map);
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.ok("删除成功");
    }

    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        userService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectMap(@PathVariable Integer id) {
        User user = userService.selectById(id);
        return Result.ok(user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.ok("修改成功");
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        userService.updateStatus(id, status);
        return Result.ok("更新成功");
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> list = userService.selectUser();
        return Result.ok(list);
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) {
        String captcha = (String) redisTemplate.opsForValue().get(CommonUtil.getCaptchaKey(request));
        if (StringUtils.isEmpty(captcha)) {
            return Result.error("验证码已失效");
        }
        if (!captcha.equalsIgnoreCase(userLoginDTO.getCaptcha())) {
            return Result.error("验证码错误");
        }
        User user = userService.login(userLoginDTO);
        if (user == null) {
            return Result.error("用户名或密码错误");
        }
        if (user.getStatus() == 0) {
            return Result.error("该用户已停用");
        }
        if (user.getDepartmentId() == 1) {
            return Result.error("请尽快联系管理员分配科室");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", user.getId());
        map.put("name", user.getName());
        String token = JwtUtil.createToken(map);
        return Result.ok("登陆成功", token);
    }

    @GetMapping("/userInfo")
    public Result userInfo(@RequestHeader(name = "Authorization") String token) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer id = (Integer) map.get("id");
        User user = userService.selectById(id);
        return Result.ok(user);
    }

    @PutMapping("/resetPassword")
    public Result resetPassword(@RequestBody UserPasswordDTO userPasswordDTO) {
        User user = userService.selectOne(userPasswordDTO);
        if (user == null) {
            return Result.error("原密码错误");
        }

        userService.updatePassword(userPasswordDTO);
        return Result.ok("更新成功");
    }

    @PutMapping("/forgotPassword")
    public Result forgotPassword(@RequestBody UserPasswordDTO userPasswordDTO) {
        User user = userService.selectByPhone(userPasswordDTO);
        if (user == null) {
            return Result.error("该手机号未有创建用户");
        }
        userService.forgotPassword(user, userPasswordDTO);
        return Result.ok("更新成功");
    }
}
